package net.sareweb.acab.entity.manager;

import javax.persistence.NoResultException;

import net.sareweb.acab.entity.Avatar;
import net.sareweb.acab.entity.User;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

@Name("avatarManager")
@Scope(ScopeType.CONVERSATION)
public class AvatarManager extends CommonManager<Avatar> {

	public static final String ICON_TYPE_BIG= "BIG";
	public static final String ICON_TYPE_MEDIUM= "MEDIUM";
	public static final String ICON_TYPE_SMALL= "SMALL";
	
	public static final int ICON_SIZE_BIG= 50;
	public static final int ICON_SIZE_MEDIUM= 25;
	public static final int ICON_SIZE_SMALL= 10;
	
	public byte[] getUsersAvatarByType(User user, String type){
		return getUsersAvatarByType(user.getIdUser(), type);
	}
	
	public byte[] getUsersAvatarByType(Integer idUser, String type){
		try{
		Avatar a = (Avatar)entityManager.createQuery(_USERS_AVATAR_BY_TYPE)
    		.setParameter("idUser",idUser)
    		.setParameter("type",type)
    		.getSingleResult();
		return a.getImage();
		}
		catch (NoResultException e) {
			return null;
		}
		
	}
	
	
	private String _USERS_AVATAR_BY_TYPE="from Avatar where user.idUser=:idUser and type=:type";
}
